package com.java01.apache.commons.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class DefineOption2 {

	public static void main(String[] args) {
		Options options = new Options();
		
		Option help = new Option( "h", "print this message" );
		Option version = new Option( "v","version", false, "display the version" );
		Option file = new Option( "f",true,"file path" );
		Option debug = new Option( "d","debug",true,"debug level" );

		options.addOption(help);
		options.addOption(version);
		options.addOption(file);
		options.addOption(debug);
		
		CommandLineParser parser = new PosixParser();
		CommandLine cmd = null;
		try {
			cmd = parser.parse( options, args);
		} catch (ParseException e) {
			System.err.println(e.getMessage());
			System.exit(1);
		}
		
		if(cmd.hasOption("-h") || args.length==0){
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp( "DefineOption2", options );
			System.exit(0);
		}
		
		if(cmd.hasOption("v")){
			System.out.println("version 1.0");
			System.exit(0);
		}
		
		if(cmd.hasOption("f")){
			System.out.println("file="+cmd.getOptionValue("f"));
		}
		
		if(cmd.hasOption("d")){
			System.out.println("debug="+cmd.getOptionValue("d"));
		}
	}
}
